package com.ecourseapp;

import android.os.Bundle;
import android.support.annotation.Nullable;

import com.ecourseapp.util.ECourseConstant;
import com.ecourseapp.util.Md5;
import com.ecourseapp.util.ScheduledExecutorManager;
import com.facebook.react.ReactActivity;
import com.facebook.react.ReactActivityDelegate;
import com.google.gson.Gson;

import java.io.IOException;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.Random;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class MainActivity extends ReactActivity {

    private Runnable runnable = new Runnable() {
        @Override
        public void run() {
            try {
                Date date = new Date();
                SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss", Locale.getDefault());
                SimpleDateFormat format2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());

                String transId = format.format(date) + getSixRandomNo();
                Request request = new Request.Builder()
                        .url("http://it.family.juziwl.cn:8090/family/user/register/login")
                        // .url("http://test.family.juziwl.cn/family/bank/listBankCards")
                        .header("SENDER", "exueyun")
                        .header("RECEIVER", "exueyunServ")
                        .header("TIME", format2.format(date))
                        .header("SERVCODE", "exueyunServ")
                        .header("MSGTYPE", "request")
                        .header("TRANSID", transId)
                        .header("VERSION", "2.0")
                        .header("SYSSIGN", Md5.MD5("exueyunServ" + transId).toLowerCase())
                        .header("clientType", "2:3:1.3.0.2")
                        .header("AccessToken", ECourseConstant.userInfo.token)
                        .get()
                        .build();
                OkHttpClient okHttpClient = new OkHttpClient();
                Response response = okHttpClient.newCall(request).execute();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    };

    private static DecimalFormat df6 = new DecimalFormat("000000");
    private static Random rand = new Random();

    /**
     * 获取6位随机码
     */
    public static String getSixRandomNo() {
        int[] array = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
        for (int i = array.length; i > 1; i--) {
            int index = rand.nextInt(i);
            int tmp = array[index];
            array[index] = array[i - 1];
            array[i - 1] = tmp;
        }
        int result = 0;
        for (int i = 0; i < 6; i++) {
            result = result * 10 + array[i];
        }
        return df6.format(result);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        ScheduledExecutorManager.getInstance().schedule(runnable, 0, 10 * 1000);
    }

    @Override
    protected void onDestroy() {
//        ScheduledExecutorManager.getInstance().cancelSchedule(runnable);
        super.onDestroy();
    }

    /**
     * Returns the name of the main component registered from JavaScript.
     * This is used to schedule rendering of the component.
     */
    @Override
    protected String getMainComponentName() {
        String eCourseFirstRouter = "eCourseApp";
        return eCourseFirstRouter;
    }

    @Override
    protected ReactActivityDelegate createReactActivityDelegate() {
        return new ReactActivityDelegate(this, getMainComponentName()) {
            @Nullable
            @Override
            protected Bundle getLaunchOptions() {
                Bundle initialProps = new Bundle();

                Map<String, Object> map = new HashMap<>();
                map.put("userInfo", ECourseConstant.userInfo);
                map.put("map", ECourseConstant.map);
                map.put("eCourseFirstRouter", ECourseConstant.eCourseFirstRouter);

                initialProps.putString("ECourseConstant", new Gson().toJson(map));
                return initialProps;
            }

        };
    }



}